<!-- 新增用户 -->
<template>
  <div class="add-user">
    <h2>{{ isEdit ? "编辑用户" : "新增用户" }}</h2>
    <form @submit.prevent="handleSubmit">
      <div class="form-item">
        <label>登录名称：</label>
        <input v-model="formData.loginName" required />
      </div>
      <div class="form-item">
        <label>员工姓名：</label>
        <input v-model="formData.empName" required />
      </div>
      <div class="form-item">
        <label>密码：</label>
        <input v-model="formData.password" required />
      </div>
      <div class="form-item">
        <label>职位：</label>
        <input v-model="formData.position" required />
      </div>
      <div class="form-item">
        <label>邮箱：</label>
        <input v-model="formData.email" type="email" required />
      </div>
      <div class="form-item">
        <label>电话号码：</label>
        <input v-model="formData.phoneNumber" required />
      </div>
      <div class="form-item">
        <label>工资：</label>
        <input v-model="formData.salary" required />
      </div>
      <div class="form-actions">
        <button type="submit">{{ isEdit ? "保存" : "新增" }}</button>
        <button type="button" @click="handleCancel">取消</button>
      </div>
    </form>
  </div>
</template>

<script>
import { saveUser, getUserById, editUser } from "@/api/user";

export default {
  name: "AddUser",
  data() {
    return {
      isEdit: false,
      formData: {
        loginName:"",
        empName: "",
        password:"",
        position: "",
        email: "",
        phoneNumber: "",
        salary:"",
        employeeId: "",
      },
    };
  },
  created() {
    // 判断是否是编辑操作
    const { type, employeeId } = this.$route.query;
    this.isEdit = type === "edit";
    if (this.isEdit && employeeId) {
      this.loadUserData(employeeId);
    }
  },
  methods: {
    // 加载用户数据
    async loadUserData(employeeId) {
      try {
        const res = await getUserById(employeeId);
        if (res.code === 200) {
          this.formData = res.data;
        }
      } catch (error) {
        console.error("加载用户数据失败：", error);
        this.$router.push("/user/list");
      }
    },
    // 提交表单
    async handleSubmit() {
      try {
        // 修改这里使用正确的 API 方法名
        const api = this.isEdit ? editUser : saveUser;
        const res = await api(this.formData);
        if (res.code === 200) {
          alert(this.isEdit ? "更新成功" : "添加成功");
          this.$router.push("/user/list");
        } else {
          alert(res.msg || (this.isEdit ? "更新失败" : "添加失败"));
        }
      } catch (error) {
        console.error("提交失败：", error);
        alert("操作失败");
      }
    },
    // 取消按钮
    handleCancel() {
      this.$router.push("/user/list");
    },
  },
};
</script>

<style lang="less" scoped>
.add-user {
  padding: 20px;

  .form-item {
    margin-bottom: 15px;

    label {
      display: inline-block;
      width: 100px;
      text-align: right;
      margin-right: 10px;
    }

    input {
      width: 200px;
      padding: 5px;
    }
  }

  .form-actions {
    margin-top: 20px;
    padding-left: 110px;

    button {
      margin-right: 10px;
      padding: 5px 15px;
    }
  }
}
</style>